<template>
  <div class="login">
    <div class="login-wrapper">
      <div class="login-wrapper_left">
        <img
          src="@/assets/images/logo.png"
          alt=""
          class="login-wrapper_left_logo"
        />
        <!-- <div class="text">让每一公里更美好</div> -->
      </div>
      <div class="login-wrapper_right">
        <el-form
          ref="loginRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          label-position="top"
        >
          <h2 class="title">欢迎使用福田卡文AI算法平台</h2>
          <el-form-item label="用户" prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              size="large"
              auto-complete="off"
              placeholder="用户"
            >
              <template #prefix
                ><svg-icon icon-class="user" class="el-input__icon input-icon"
              /></template>
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              size="large"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter="handleLogin"
            >
              <template #prefix
                ><svg-icon
                  icon-class="password"
                  class="el-input__icon input-icon"
              /></template>
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              class="button"
              :loading="loading"
              size="large"
              type="primary"
              style="width: 100%"
              @click.prevent="handleLogin"
            >
              <span v-if="!loading">{{ register ? "注 册" : "登 录" }}</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "admin",
  password: "admin123",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
};
const loading = ref(false);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      // if (loginForm.value.rememberMe) {
      //   Cookies.set("username", loginForm.value.username, { expires: 30 });
      //   Cookies.set("password", encrypt(loginForm.value.password), {
      //     expires: 30,
      //   });
      // } else {
      //   // 否则移除
      //   Cookies.remove("username");
      //   Cookies.remove("password");
      // }
      // 调用action的登录方法
      // userStore
      //   .login(loginForm.value)
      //   .then(() => {
      //     const query = route.query;
      //     const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
      //       if (cur !== "redirect") {
      //         acc[cur] = query[cur];
      //       }
      //       return acc;
      //     }, {});
      //     router.push({ path: redirect.value || "/", query: otherQueryParams });
      //   })
      //   .catch(() => {
      //     loading.value = false;
      //   });

      //  纯前端登录
      const query = route.query;
      const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
      router.push({
        path: "/resource",
        query: otherQueryParams,
      });
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

const goRegister = () => {
  register.value = !register.value;
};

// getCode();
getCookie();
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  min-height: 100vh;
  background: #01272c;
  position: relative;
  display: flex;
  .login-wrapper {
    width: 1300px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    display: flex;
    z-index: 1;
    &_left {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background: #003c43;
      &_logo {
        width: 200px;
      }
      .text {
        font-size: 22px;
        color: #e8f7ea;
      }
    }
    &_right {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 84px 0 80px 0;
      .title {
        color: rgba(0, 60, 67, 1);
      }
    }
  }
}

.login-form {
  .logo {
    width: 400px;
    margin-bottom: 40px;
  }
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
  .button {
    background: rgba(0, 60, 67, 1);
    border: none;
    margin-top: 40px;
    color: rgba(255, 255, 255, 1);
  }
}
:deep(.el-form-item__label) {
  color: rgba(48, 49, 51, 1);
}
</style>
